<template>
  <div class="app-container">
    <div class="filter-container">
        <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入账号" v-model="listQuery.name"></el-input>
        <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加</el-button>
    </div>
    <el-table :data="userList" border style="width: 100%;" >
      <el-table-column prop="id" label="ID">
      </el-table-column>
      <el-table-column prop="name" label="账号" >
      </el-table-column>
      <el-table-column prop="password" label="密码">
      </el-table-column>
      <el-table-column align="center" label="操作" width="260" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteTest(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="70px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="temp.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="temp.password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
        <el-button v-else type="primary" @click="updateData">更新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getTest, deleteTest, addTest, updateTest } from '@/api/test'
  import waves from '@/directive/waves'
  export default {
    directives: {
      waves
    },
    data() {
      return {
        userList: null,
        total: null,
        test: {
          id: null
        },
        listQuery: {
          page: 1,
          size: 10,
          name: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: 'Edit',
          create: 'Create'
        },
        dialogPvVisible: false,
        temp: {
          id: undefined,
          name: '',
          password: ''
        },
        rules: {
          name: [{ required: true, message: 'name is required', trigger: 'blur' }],
          password: [{ required: true, message: 'password is required', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.getTestList()
    },
    methods: {
      handleFilter() {
        this.listQuery.page = 1
        this.getTestList()
      },
      deleteTest(row) {
        this.test.id = row.id
        this.$confirm('此操作将永久性删除，是否继续', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteTest(this.test).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            const index = this.userList.indexOf(row)
            this.userList.splice(index, 1)
          })
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      },
      getTestList() {
        getTest(this.listQuery).then(response => {
          this.userList = response.data.rows
          this.total = response.data.total
        })
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getTestList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getTestList()
      },
      resetTemp() {
        this.temp = {
          id: undefined,
          name: '',
          password: ''
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addTest(this.temp).then(() => {
              this.userList.unshift(this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updateTest(this.temp).then(() => {
              for (const v of this.userList) {
                if (v.id === this.temp.id) {
                  const index = this.userList.indexOf(v)
                  this.userList.splice(index, 1, this.temp)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
  .app-container{
    padding: 30px;
  }
</style>
